﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FileMergerApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSelectFirstFile_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "Text Files (*.txt)|*.txt";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    txtFirstFile.Text = openFileDialog.FileName;
                }
            }
        }

        private void btnSelectSecondFile_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "Text Files (*.txt)|*.txt";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    txtSecondFile.Text = openFileDialog.FileName;
                }
            }
        }

        private void btnMergeFiles_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtFirstFile.Text) && !string.IsNullOrEmpty(txtSecondFile.Text))
            {
                try
                {
                    string dataDirectory = Path.Combine(Application.StartupPath, "Data");

                    if (!Directory.Exists(dataDirectory))
                    {
                        Directory.CreateDirectory(dataDirectory);
                    }
                    string mergedFilePath = Path.Combine(dataDirectory, "MergedFile.txt");

                    string firstFileContent = File.ReadAllText(txtFirstFile.Text);
                    string secondFileContent = File.ReadAllText(txtSecondFile.Text);
                    File.WriteAllText(mergedFilePath, firstFileContent + Environment.NewLine + secondFileContent);

                    lblStatus.Text = dataDirectory;
                }
                catch (Exception ex)
                {
                    lblStatus.Text = "An error occurred: " + ex.Message;
                }
            }
            else
            {
                lblStatus.Text = "Please select both files first!";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {

        }
    }
}
